package com.qiqu.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import java.io.IOException;

//发送请求
public class AuthUtil {
    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.fromObject(result);
        }
        httpGet.releaseConnection();
        return jsonObject;
    }

    public static void main(String[] args) throws IOException {
        //根据code获取唯一的微信openId
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxb6cc718c583b3bb2&secret=798e638e29005ef6c17ce42df58b7b4a&js_code=043QrC0w31kwrW2Ku91w3gR2890QrC0-&grant_type=authorization_cod";

        JSONObject jsonObject = AuthUtil.doGetJson(url);

    }
}

